#include "CTest.hpp"
#include <stdexcept>
#include <fstream>
#include <sstream>
#include <iomanip>
using namespace std;


CTest::CTest()
{
}

void CTest::testPoint()
{
    size_t nDim = 3;
    size_t id = 0;
    CEntity* point = new CPoint(id, nDim);
    double* CG = new double[nDim];
    for(size_t iDim = 0; iDim < nDim; iDim++) CG[iDim] = iDim;
    point->setCG(CG);

    cout << "------------------------------test point------------------------------" << endl;
    cout << "id     :    "<< point->getId() << endl;
    cout << "nPoint :    "<< point->getNPoint() << endl;
    cout << "nDim   :    "<< point->getNDim() << endl;
    cout << "CG     :    ";
    for(size_t iDim = 0; iDim < nDim; iDim++)
        cout << point->getCG(iDim) << "    ";
    cout << endl;

    point->~CEntity();



    delete[] CG;
}

void CTest::testLine()
{
    size_t nDim = 2;
    size_t nPoint = 2;
    size_t id = 1;
    CEntity** point = new CEntity*[nPoint];
    point[0] = new CPoint(0, nDim);
    point[1] = new CPoint(1, nDim);

    double* CG = new double[nDim];
    CG[0] = 0.0; CG[1] = 1.0;
    point[0]->setCG(CG);
    CG[0] = 1.0; CG[1] = 2.0;
    point[1]->setCG(CG);

    CEntity* line = new CLine(id, nDim);
    line->init(point);
    cout << "------------------------------test line------------------------------" << endl;
    cout << "id     :    "<< line->getId() << endl;
    cout << "nPoint :    "<< line->getNPoint() << endl;
    cout << "nDim   :    "<< line->getNDim() << endl;
    cout << "CG     :    ";
    for(size_t iDim = 0; iDim < nDim; iDim++)
        cout << line->getCG(iDim) << "    ";
    cout << endl;
    cout << "point  :    "<< line->getPoint(0) << "   "<< line->getPoint(1) << endl;


    delete[] CG;
    line->~CEntity();

    delete point[0];
    delete point[1];
    delete[] point;

}

void CTest::testGeo()
{
    string filename;
    cout << "Please input the config-file name:" << endl;
    cin >> filename;
    CConfig* config = new CConfig(filename);
    config->init();

    CGeometry* geo = new CGeometry();
    geo->loadmesh(config);
    cout << "------------------------------test geo------------------------------" << endl;
    geo->savemesh(config);
    cout << "------------------------------test interface------------------------------" << endl;
    geo->setPointNeighborPoint();
    geo->setInterfaceNormal();
    geo->setElemVolume();
    geo->setBound(config);
    filename = "interfaceNode.dat";
    ofstream out;
    out.open(filename.c_str());
    if(out.is_open())
    {
        for(size_t iFace = 0; iFace < geo->getNIface(); iFace++)
        {
            out << geo->interface[iFace]->getId() << "  ";
            out << geo->interface[iFace]->getBoundType() << "  ";
            for(size_t iPoint = 0; iPoint < geo->interface[iFace]->getNPoint(); iPoint++)
            {
                out << geo->interface[iFace]->getPoint(iPoint) << "  ";
            }

            for(size_t iDim = 0; iDim < geo->getNDim(); iDim++)
            {
                out << geo->interface[iFace]->getCG(iDim) << "  ";
            }
            out << endl;
        }

        out.close();
    } else
    {
        throw runtime_error("The file " + filename + " is not opened correctly!\n");
    }

    filename = "interfaceParent.dat";
    out.open(filename.c_str());
    if(out.is_open())
    {
        for(size_t iFace = 0; iFace < geo->getNIface(); iFace++)
        {
            out << geo->interface[iFace]->getId() << "  ";
            for(size_t iPoint = 0; iPoint < geo->interface[iFace]->getNParent(); iPoint++)
            {
                out << geo->interface[iFace]->getParent(iPoint) << "  ";
            }

            for(size_t iDim= 0; iDim < geo->interface[iFace]->getNDim(); iDim++)
            {
                out << geo->interface[iFace]->normal[iDim] << "  ";
            }

            out << endl;
        }
        out.close();
    } else
    {
        throw runtime_error("The file " + filename + " is not opened correctly!\n");
    }

    filename = "elem.dat";
    out.open(filename.c_str());
    if(out.is_open())
    {
        for(size_t iElem = 0; iElem < geo->getNElem(); iElem++)
        {
            out << geo->elem[iElem]->getId() << "  ";
            for(size_t iPoint = 0; iPoint < geo->elem[iElem]->getNPoint(); iPoint++)
            {
                out << geo->elem[iElem]->getPoint(iPoint) << "  ";
            }

            for(size_t iDim = 0; iDim < geo->getNDim(); iDim++)
            {
                out << geo->elem[iElem]->getCG(iDim) << "  ";
            }

            out << geo->elem[iElem]->getVolume() << "   ";
            out << endl;
        }
        out.close();
    } else
    {
        throw runtime_error("The file " + filename + " is not opened correctly!\n");
    }
}

void CTest::testConfig()
{
    string filename;
    cout << "Please input the config filename:\n" << endl;
    cin >> filename;
    CConfig config(filename);
    config.init();
    cout << "nDim            = " << config.getNDim()<< endl;
    cout << "meshFile        = " << config.getMeshFile() << endl;
    cout << "outMeshFile     = " << config.getOutMeshFile() << endl;
    cout << "KindSolver      = " << config.getKindSolver()<< endl;

    cout << "nMarker         = " << config.getNMarker() << endl;
    for(long int iMarker = 0; iMarker < config.getNMarker(); iMarker++)
    {
        cout << "Marker"<< iMarker << "         = " << config.getMarkerType(iMarker) << endl;
    }
}
